<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS实现购物车结算功能代码</title>
    <link rel="stylesheet" href="../css/style.css" />
</head>

<body>
    <div class="catbox">
        <table id="cartTable">
            <thead>
                <tr>
                    <th><label>
                            <input class="check-all check" type="checkbox" />&nbsp;&nbsp;全选</label></th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/1.jpg" alt="" /><span>Casio/卡西欧 EX-TR350</span></td>
                    <td class="price">5999.88</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">5999.88</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/2.jpg" alt="" /><span>Canon/佳能 PowerShot SX50 HS</span></td>
                    <td class="price">3888.50</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">3888.50</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/3.jpg" alt="" /><span>Sony/索尼 DSC-WX300</span></td>
                    <td class="price">1428.50</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">1428.50</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/4.jpg" alt="" /><span>Fujifilm/富士 instax mini 25</span></td>
                    <td class="price">640.60</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">640.60</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
            </tbody>
        </table>
        <div class="foot" id="foot">

            <a class="fl" id="deleteAll" href="javascript:;">删除</a>
            <div class="fr closing" onclick="getTotal();">结 算</div>
            <input type="hidden" id="cartTotalPrice" />
            <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
            <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span>件<span
                    class="arrow up">︽</span><span class="arrow down">︾</span></div>
            <div class="selected-view">
                <div id="selectedViewList" class="clearfix">
                    <div><img src="../images/1.jpg"><span>取消选择</span></div>
                </div>
                <span class="arrow">◆<span>◆</span></span>
            </div>
        </div>
    </div>
</body>
<script>
    // var addList = document.getElementsByClassName("add");
    // var reduceList = document.getElementsByClassName("reduce");
    // var deleteList = document.getElementsByClassName("delete");
    // var deleteAll = document.getElementById("deleteAll");
    // var selectedTotal = document.getElementById("selectedTotal");
    // var priceTotal = document.getElementById("priceTotal");

    var catbox = document.getElementsByClassName("catbox")[0];
    var checkAll = document.getElementsByClassName("check-all")[0];
    var checkOneList = document.getElementsByClassName("check-one");

    // 注意: 事件委托,只是将子元素的事件委托给父元素,由父元素代为执行, 该获取的元素还是要获取

    catbox.addEventListener("click", entrustHandler);

    function entrustHandler(e) {
        // this => 绑定事件的元素 catbox
        // console.log(this,e);
        e = e || window.event;
        var target = e.target;  // 触发事件的元素(事件源)
        console.log(target);

        if (target.classList.contains("check-all")) {
            console.log("全选");
            var status = target.checked;
            for (var i = 0; i < checkOneList.length; i++) {
                var checkOne = checkOneList[i];
                checkOne.checked = status;
            }
        } else if (target.classList.contains("check-one")) {
            console.log("单选");

            var flag = Array.from(checkOneList).every(function (checkOne) {
                return checkOne.checked;
            })

            checkAll.checked = flag;

        } else if (target.className == "add") {
            console.log("加");
            //a. 输入框  数量 加一 
            var countInput = target.previousElementSibling;
            var num = countInput.value;
            num++;
            countInput.value = num;
            // countInput.value ++;
            console.log(num);

            //b. 小计更新  =  单价*数量
            var priceTd = target.parentElement.previousElementSibling;
            var subtotalTd = target.parentElement.nextElementSibling;
            console.log(priceTd, subtotalTd);

            var price = priceTd.textContent;
            subtotalTd.textContent = (price * num).toFixed(2);

            // c  减号按钮的 “-”  出现
            var reduce = countInput.previousElementSibling;
            reduce.textContent = "-";

        } else if (target.className == "reduce") {
            // a. 输入框  数量 减一 
            var countInput = next(target);
            var num = countInput.value;
            // c. 减之前判断 如果商品数量为1,不能减
            if (num == 1) {
                alert("亲,不能再少了!");
                return false;  // 阻止函数继续向后执行
            }

            num--;

            // d. 减之后判断, 如果商品数量为1 (2切换为1), "-"消失
            if (num == 1) {
                target.textContent = "";
            }


            countInput.value = num;

            // b. 小计更新  =  单价*数量
            var priceTd = prev(parent(target));
            var subtotalTd = next(parent(target));
            console.log(priceTd, subtotalTd);

            var price = priceTd.textContent;
            subtotalTd.textContent = (price * num).toFixed(2);
        } else if (target.className == "delete") {
            console.log("单删");
            // console.log(this, del, i);
            var bool = confirm("是否删除该商品");
            // console.log(bool);
            if (bool) {
                var parentTr = parent(parent(target));
                parentTr.remove();
            }
            console.log(checkOneList);

            isAllChecked();


        } else if (target.id == "deleteAll") {
            console.log("全删");
            for (var j = 0; j < checkOneList.length; j++) {
                var checkOne = checkOneList[j];
                if (checkOne.checked) {
                    var tr = parent(parent(checkOne));
                    tr.remove();
                    j--;
                }
            }
            // checkAll.checked = false;
            isAllChecked();
        }
    }



    function isAllChecked() { // 判断是否反选

        var flag = true; // 假设全都被选中
        for (var j = 0; j < checkOneList.length; j++) {
            var item = checkOneList[j];
            if (!item.checked) {
                flag = false;
                break;
            }
        }

        // 判断是否有商品  => 有商品就是用flag,没有商品 checkAll.checked = false;
        checkAll.checked = checkOneList.length == 0 ? false : flag;
    }

    function getTotal() {
        var sum = 0;
        var allPrice = 0;

        for (var j = 0; j < checkOneList.length; j++) {
            var item = checkOneList[j];  // 每一个单选
            if (item.checked) {
                var tr = parent(parent(item));
                var countInput = tr.getElementsByClassName("count-input")[0];
                var subtotalTd = tr.getElementsByClassName("subtotal")[0];

                var num = countInput.value * 1;
                var subtotal = subtotalTd.textContent * 1;

                sum += num;
                allPrice += subtotal;

            }
        }

        selectedTotal.textContent = sum;
        priceTotal.textContent = allPrice.toFixed(2);
    }

    function parent(ele) {
        return ele.parentElement;
    }

    function prev(ele) {
        return ele.previousElementSibling;
    }

    function next(ele) {
        return ele.nextElementSibling;
    }


</script>

</html>